/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastroAulasDialog.java
 *
 * Created on 07/11/2009, 19:47:30
 */
package interpretadorportugolvisual.cadastros;

import br.org.isvi.interpretador.portugol.access.IPVBusinessFacade;
import br.org.isvi.interpretador.portugol.model.Aula;
import interpretadorportugolvisual.InterpretadorPortugolVisualApp;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author abraaoisvioliveirasouzadocarmo
 */
public class CadastroAulasDialog extends javax.swing.JDialog {

    /** Creates new form CadastroAulasDialog */
    public CadastroAulasDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

//        tblAulas.getModel().addTableModelListener(this);
        tblAulas.setCellSelectionEnabled(true);
        tblAulas.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnSair = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblAulas = new javax.swing.JTable();
        btnAdicionar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(interpretadorportugolvisual.InterpretadorPortugolVisualApp.class).getContext().getResourceMap(CadastroAulasDialog.class);
        btnSair.setText(resourceMap.getString("btnSair.text")); // NOI18N
        btnSair.setName("btnSair"); // NOI18N
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblAulas.setModel(obterAulas());
        tblAulas.setName("tblAulas"); // NOI18N
        jScrollPane1.setViewportView(tblAulas);

        btnAdicionar.setText(resourceMap.getString("btnAdicionar.text")); // NOI18N
        btnAdicionar.setName("btnAdicionar"); // NOI18N
        btnAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAdicionar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 275, Short.MAX_VALUE)
                        .addComponent(btnSair)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSair)
                    .addComponent(btnAdicionar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarActionPerformed
        NovaAulaDialog dlg = new NovaAulaDialog(InterpretadorPortugolVisualApp.getApplication().getMainFrame(), true);

        dlg.setVisible(true);
        
        Aula t = dlg.getAula();
        IPVBusinessFacade.persistir();

        ((DefaultTableModel) tblAulas.getModel()).addRow(new Aula[]{t,t});
    }//GEN-LAST:event_btnAdicionarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnSairActionPerformed

    public DefaultTableModel obterAulas() {
        //tblAulas.setModel(new DefaultTableModel(IPVBusinessFacade.obterTodos(new Aula()).toArray(), new String[]{"Descrição"}));
        java.util.List<Aula> lst = IPVBusinessFacade.obterTodosAula();
        Aula[][] Aulas = new Aula[lst.size()][2];

        for (int i = 0; i < Aulas.length; i++) {
            Aulas[i] = new Aula[]{lst.get(i), lst.get(i)};
        }


        return new DefaultTableModel(Aulas, new String[]{"Número", "Descrição"}) {

            @Override
            public Object getValueAt(int row, int col) {

                if(super.getRowCount() <= 0)
                    return null;

                if (col == 0) {
                    return ((Aula) super.getValueAt(row, 0)).getNumero().toString();
                } else {

                    return super.getValueAt(row, 1);
                }
            }

            @Override
            public void setValueAt(Object arg0, int row, int col) {
                Aula t = (Aula) getValueAt(row, 1);

                if(col > 0) {
                    t.setDescricao(arg0.toString());
                } else {
                    t.setNumero(Integer.parseInt(arg0.toString()));
                }

                IPVBusinessFacade.persistir();

                fireTableCellUpdated(row, col);
            }
        };
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdicionar;
    private javax.swing.JButton btnSair;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblAulas;
    // End of variables declaration//GEN-END:variables

//    public void tableChanged(TableModelEvent e) {
////        int row = e.getFirstRow();
////        int column = e.getColumn();
////        DefaultTableModel model = (DefaultTableModel)e.getSource();
////
////        Object data = model.getValueAt(row, column);
////
////        Aula t = new Aula();
////        t.descricao = data.toString();
////
////        t = IPVBusinessFacade.inserir(t);
//
//    }
}
